package com.shujia.day14;

public class Demo07ThreadGroup {
    public static void main(String[] args) {
        /*
            TODO 线程组

         */

        ThreadGroup threadGroup1 = new ThreadGroup("帅哥组");
        ThreadGroup threadGroup2 = new ThreadGroup("美女组");


        MyThread thread1 = new MyThread(threadGroup2, "徐玉娟");
        MyThread thread2 = new MyThread(threadGroup2, "袁笑琪");
        thread1.start();
        thread2.start();

        MyThread thread3 = new MyThread(threadGroup1, "易政");
        MyThread thread4 = new MyThread(threadGroup1, "汪雨");
        thread3.start();
        thread4.start();


        threadGroup2.setDaemon(true); // 设置为守护线程
        System.out.println(threadGroup2.isDaemon());


    }

    static class MyThread extends Thread {
        public MyThread(ThreadGroup group, String name) {
            super(group, name);
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                if (i >= 10 && this.getThreadGroup().getName() == "美女组") {
                    this.getThreadGroup().stop();  // 可以将整个线程组的线程全部关闭
                }
                System.out.println(this.getThreadGroup().getName()+"线程名称:"+this.getName() + "正在打印第" + i + "个值");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
